import { Layers, Node, SpriteFrame, UITransform } from 'cc'

export const createUINode = (name: string = '') => {
    const node = new Node(name)
    const transform = node.addComponent(UITransform)
    transform.setAnchorPoint(0, 1)
    node.layer = 1 << Layers.nameToLayer("UI_2D")
    return node
}

// 生成指定长度随机uuid
export const randomByLength = (n: number) =>
    Array.from({ length: n }).reduce<string>((total: string) => total + Math.floor(Math.random() * 10), '')


export const randomByRange = (start: number, end: number) => Math.floor(start + (end - start) * Math.random())

const spriteFrameNumberReg = /\((\d+)\)/

const getNumberWithinSpriteFrameName = (str: string) => parseInt(str.match(spriteFrameNumberReg)?.[1] || '0')


export const sortSpriteFrame = (spriteFrames: SpriteFrame[]) => {
    return spriteFrames.sort((a, b) => getNumberWithinSpriteFrameName(a.name) - getNumberWithinSpriteFrameName(b.name))
}
